<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.9.6"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>AngelScript: Mixin class</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
  $(document).ready(function() { init_search(); });
/* @license-end */
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr id="projectrow">
  <td id="projectlogo"><img alt="Logo" src="aslogo_small.png"/></td>
  <td id="projectalign">
   <div id="projectname">AngelScript
   </div>
  </td>
    <td>        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <span id="MSearchSelect"                onmouseover="return searchBox.OnSearchSelectShow()"                onmouseout="return searchBox.OnSearchSelectHide()">&#160;</span>
          <input type="text" id="MSearchField" value="" placeholder="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.svg" alt=""/></a>
          </span>
        </div>
</td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.6 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search/",'.html');
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(document).ready(function(){initNavTree('doc_script_mixin.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>

<div><div class="header">
  <div class="headertitle"><div class="title">Mixin class </div></div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>As <a class="el" href="doc_script_class_inheritance.html">multiple inheritance</a> is not available, it may sometimes be necessary to implement identical code in multiple classes. When this is necessary it is recommended to use mixin classes to avoid writing the identical code in multiple places.</p>
<p>Mixin classes allows a script to declare a partial class structure that will be included into multiple different class declarations. The mixin classes themselves are not real types and cannot be instanciated.</p>
<p>When a mixin class is included into a class declaration, the properties and methods that were declared in the mixin class will be automatically replicated into the class.</p>
<pre>
  // Declare a mixin class
  mixin class MyMixin
  {
    void SomeMethod() { property++; }
    int property;
  }

  // Include the mixin class into the class to receive the methods and properties
  class MyClass : MyMixin
  {
    int OtherMethod()
    {  
      SomeMethod();
      return property;
    }
  }
</pre><p>Properties and methods that have already been explicitly declared in the class will not be included again. This way the mixin class can provide a default implementation that can be overridden by the class that includes the mixin.</p>
<p>The class methods included from a mixin class will be compiled in the context of the class that included it, so it is possible for a mixin class method to refer to properties and other methods that are not declared in the mixin class if the class that includes the mixin class provides those.</p>
<pre>
  mixin class MyMixin
  {
    void MethodA() { print("Default behaviour"); } 
    void MethodB() { property++; }
  }

  class MyClass : MyMixin
  {
    // Override the default behaviour in MethodA
    void MethodA() { print("Overridden behaviour"); }

    // Declare the property that will be used by MethodB
    int property;
  }
</pre><p>Mixin class methods override inherited methods from base classes, just as if the included method had been implemented in the derived class directly. Mixin class properties on the other hand are not included if the property is already inherited from a base class.</p>
<pre>
  class MyBase
  {
    void MethodA() { print("Base behaviour"); }
    int property;
  }

  mixin class MyMixin
  {
    void MethodA() { print("Mixin behaviour"); }
    float property;
  }

  // Inheriting from base class and including mixin.
  // MyClass ends up with the property from the base 
  // class and the method from the mixin class.
  class MyClass : MyBase, MyMixin
  {
  }
</pre><p>A mixin class can inform a list of interfaces that has to be implemented by the script class that includes the mixin class. In this case the methods for the interfaces can optionally be provided by the mixin class itself, or left out to be implemented by the script class directly.</p>
<p>A mixin class cannot inherit from other classes.</p>
<pre>
  interface I 
  {
    void a();
    void b();   
  }

  mixin class M : I
  {
        // provide default implementation of a()
    void a() { print("hello from a");

    // leave the implementation of b() to the script class
  }

  class C : M
  {
    // a() is implemented by mixin class

        // b() has to be explicitly implemented by script class
        void b() { print("hello from b");
  }
</pre> </div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated on Sun May 21 2023 10:12:40 for AngelScript by <a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.6 </li>
  </ul>
</div>
</body>
</html>
